package com.example.tutorial.reminder;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

import com.example.tutorial.model.DaoMaster;
import com.example.tutorial.model.DaoMaster.DevOpenHelper;
import com.example.tutorial.model.DaoSession;
import com.example.tutorial.model.Task;
import com.example.tutorial.model.TaskDao;
import com.example.tutorial.mytodolist.AddTaskActity;
import com.example.tutorial.mytodolist.R;
import com.example.tutorial.ultils.Constant;

/**
 * Xay dung notification
 * 
 * @author vanch_000
 * 
 */
public class ReminderService extends WakeReminderIntentService {

	public ReminderService(String name) {
		super("ReminderService");
	}

	@SuppressWarnings("deprecation")
	@Override
	void doReminderWork(Intent intent) {
		Long taskId = intent.getExtras().getLong(Constant.KEY_TASKID);
		DevOpenHelper helper = new DevOpenHelper(getApplicationContext(),
				"task-db", null);
		SQLiteDatabase db = helper.getWritableDatabase();
		DaoMaster daoMaster = new DaoMaster(db);
		DaoSession daoSession = daoMaster.newSession();
		TaskDao taskDao = daoSession.getTaskDao();
		Task task = taskDao.loadByRowId(taskId);
		if (task != null) {
			NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			Intent notificationIntent = new Intent(getApplicationContext(),
					AddTaskActity.class);
			notificationIntent.putExtra(Constant.KEY_TASKID, taskId);

			PendingIntent pi = PendingIntent.getActivity(
					getApplicationContext(), 0, notificationIntent,
					PendingIntent.FLAG_ONE_SHOT);
			
			Notification note=new Notification(android.R.drawable.stat_sys_warning, 
					 getString(R.string.notify_new_task_message),
					 System.currentTimeMillis()); 
			note.setLatestEventInfo(this, getString(R.string.notifiy_new_task_title), 
					 getString(R.string.notify_new_task_message), pi);
			note.defaults |= Notification.DEFAULT_SOUND;
			note.flags |= Notification.FLAG_AUTO_CANCEL; 
			int id = taskId.intValue();
			mgr.notify(id, note); 
		}
	}

}
